package com.clgc.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clgc.eduservice.entity.EduTopic;
import com.clgc.eduservice.service.EduTopicService;
import com.clgc.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.HEAD;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@Api("题库管理模块")
@RestController
@RequestMapping("/eduservice/edu-topic")
public class EduTopicController {

    @Autowired
    EduTopicService topicService;


    @ApiOperation("手动添加题库信息")
    @PostMapping("addTopic")
    public R addTopic(@RequestBody EduTopic eduTopic) {
        boolean flag = topicService.save(eduTopic);
        return flag ? R.ok() : R.error().message("题库添加失败");
    }

    @ApiOperation("文件导入题库")
    @PostMapping("addTopicByFile")
    public R addTopicByFile(MultipartFile file) {

        topicService.saveFile(file, topicService);

        return R.ok();
    }

    @ApiOperation("通过id删除题库")
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id) {

        boolean flag = topicService.removeById(id);
        return flag ? R.ok() : R.error().message("题库删除失败");
    }

    @ApiOperation("删除课程下的题库")
    @DeleteMapping("deleteByCourseId/{courseId}")
    public R deleteByCourseId(@PathVariable String courseId) {
        QueryWrapper<EduTopic> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id", courseId);
        boolean flag = topicService.remove(queryWrapper);
        return flag ? R.ok() : R.error().message("删除课程题库失败");
    }

    @ApiOperation("修改题库信息")
    @PostMapping("updateTopic")
    public R updateById(@RequestBody EduTopic eduTopic) {

        boolean flag = topicService.updateById(eduTopic);
        return flag ? R.ok() : R.error().message("题库修改失败");
    }

    @ApiOperation("分页查询题库信息")
    @GetMapping("getTopicAsPage/{page}/{size}")
    public R getTopicAsPage(@PathVariable Long page, @PathVariable Long size) {
        Page<EduTopic> topicPage = new Page(page, size);

        QueryWrapper<EduTopic> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");

        topicService.page(topicPage, queryWrapper);
        long total = topicPage.getTotal();
        List<EduTopic> records = topicPage.getRecords();
        long current = topicPage.getCurrent();
        boolean hasNext = topicPage.hasNext();
        boolean hasPrevious = topicPage.hasPrevious();
        if (records.size() == 0) {
            return R.error().message("查询题库失败");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        map.put("current", current);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return R.ok().data(map);
    }

    @ApiOperation("分页查询课程下的题库信息")
    @GetMapping("getTopicAsPageByCourseId/{page}/{size}/{courseId}")
    public R getTopicAsPageByCourseId(@PathVariable Long page, @PathVariable Long size, @PathVariable String courseId) {

        Page<EduTopic> topicPage = new Page(page, size);
        QueryWrapper<EduTopic> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.orderByDesc("create_time");

        topicService.page(topicPage, queryWrapper);
        long total = topicPage.getTotal();
        List<EduTopic> records = topicPage.getRecords();
        long current = topicPage.getCurrent();
        boolean hasNext = topicPage.hasNext();
        boolean hasPrevious = topicPage.hasPrevious();
        if (records.size() == 0) {
            return R.error().message("查询题库失败");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        map.put("current", current);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return R.ok().data(map);

    }


}



